<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: joydriveActionExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.4</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('joydriveActionExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">joydriveActionExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Example <a class="el" href="classArAction.html" title="Base class for actions. ">ArAction</a> object that uses <a class="el" href="classArJoyHandler.html" title="Interfaces to a computer joystick. ">ArJoyHandler</a> to get joystick input.This program just drives the robot around with a joystick, by way of an <a class="el" href="classArAction.html" title="Base class for actions. ">ArAction</a> class which reads data fram an <a class="el" href="classArJoyHandler.html" title="Interfaces to a computer joystick. ">ArJoyHandler</a>. Its a practical example of actions.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"><span class="comment">// the action which will drive the robot</span></div><div class="line"><span class="keyword">class </span>JoydriveAction : <span class="keyword">public</span> <a name="_a0"></a><a class="code" href="classArAction.html">ArAction</a></div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">  <span class="comment">// constructor</span></div><div class="line">  JoydriveAction(<span class="keywordtype">void</span>);</div><div class="line">  <span class="comment">// empty destructor</span></div><div class="line">  <span class="keyword">virtual</span> ~JoydriveAction(<span class="keywordtype">void</span>);</div><div class="line">  <span class="comment">//the fire which will actually tell the resolver what to do</span></div><div class="line">  <span class="keyword">virtual</span> <a name="_a1"></a><a class="code" href="classArActionDesired.html">ArActionDesired</a> *<a name="a2"></a><a class="code" href="classArAction.html#a900675ede8563489ccc2e7e0934670bb">fire</a>(<a class="code" href="classArActionDesired.html">ArActionDesired</a> currentDesired);</div><div class="line">  <span class="comment">// whether the joystick is initalized or not</span></div><div class="line">  <span class="keywordtype">bool</span> joystickInited(<span class="keywordtype">void</span>);</div><div class="line"><span class="keyword">protected</span>:</div><div class="line">  <span class="comment">// action desired</span></div><div class="line">  <a class="code" href="classArActionDesired.html">ArActionDesired</a> myDesired;</div><div class="line">  <span class="comment">// joystick handler</span></div><div class="line">  <a name="_a3"></a><a class="code" href="classArJoyHandler.html">ArJoyHandler</a> myJoyHandler;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  Note the use of constructor chaining with ArAction.</span></div><div class="line"><span class="comment">*/</span>  </div><div class="line">JoydriveAction::JoydriveAction(<span class="keywordtype">void</span>) :</div><div class="line">  <a name="a4"></a><a class="code" href="classArAction.html#a4341cda2464899c839e370b514af929b">ArAction</a>(<span class="stringliteral">&quot;Joydrive Action&quot;</span>, <span class="stringliteral">&quot;This action reads the joystick and sets the translational and rotational velocity based on this.&quot;</span>)</div><div class="line">{</div><div class="line">  <span class="comment">// initialize the joystick</span></div><div class="line">  myJoyHandler.init();</div><div class="line">  <span class="comment">// set up the speed parameters on the joystick</span></div><div class="line">  myJoyHandler.setSpeeds(50, 700);</div><div class="line">}</div><div class="line"></div><div class="line">JoydriveAction::~JoydriveAction(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// whether the joystick is there or not</span></div><div class="line"><span class="keywordtype">bool</span> JoydriveAction::joystickInited(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  <span class="keywordflow">return</span> myJoyHandler.haveJoystick();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// the guts of the thing</span></div><div class="line"><a class="code" href="classArActionDesired.html">ArActionDesired</a> *JoydriveAction::fire(<a class="code" href="classArActionDesired.html">ArActionDesired</a> currentDesired)</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> rot, trans;</div><div class="line"></div><div class="line">  <span class="comment">// print out some info about hte robot</span></div><div class="line">  printf(<span class="stringliteral">&quot;\rx %6.1f  y %6.1f  tth  %6.1f vel %7.1f mpacs %3d&quot;</span>, <a class="code" href="classArAction.html#a265851e04729e51a2a72d1ab4f1c5ed8">myRobot</a>-&gt;<a name="a5"></a><a class="code" href="group__easy.html#ga4c994926aaefc4c86380f1a74e508a76">getX</a>(),</div><div class="line">     <a class="code" href="classArAction.html#a265851e04729e51a2a72d1ab4f1c5ed8">myRobot</a>-&gt;<a name="a6"></a><a class="code" href="group__easy.html#ga9164db66102fdf9d422feefdadffca92">getY</a>(), <a class="code" href="classArAction.html#a265851e04729e51a2a72d1ab4f1c5ed8">myRobot</a>-&gt;<a name="a7"></a><a class="code" href="group__easy.html#gaefe932449c50da9ca0f8084cd9639583">getTh</a>(), <a class="code" href="classArAction.html#a265851e04729e51a2a72d1ab4f1c5ed8">myRobot</a>-&gt;<a name="a8"></a><a class="code" href="group__easy.html#gad75dbd9aede7b7f57c9a38902b8aa1ce">getVel</a>(), </div><div class="line">     <a class="code" href="classArAction.html#a265851e04729e51a2a72d1ab4f1c5ed8">myRobot</a>-&gt;<a name="a9"></a><a class="code" href="classArRobot.html#a9eb53fc27147cd3467fa3254cbb829f0">getMotorPacCount</a>());</div><div class="line">  fflush(stdout);</div><div class="line"></div><div class="line">  <span class="comment">// see if one of the buttons is pushed, if so drive</span></div><div class="line">  <span class="keywordflow">if</span> (myJoyHandler.haveJoystick() &amp;&amp; (myJoyHandler.getButton(1) ||</div><div class="line">                    myJoyHandler.getButton(2)))</div><div class="line">  {</div><div class="line">    <span class="comment">// get the readings from the joystick</span></div><div class="line">    myJoyHandler.getAdjusted(&amp;rot, &amp;trans);</div><div class="line">    <span class="comment">// set what we want to do</span></div><div class="line">    myDesired.setVel(trans);</div><div class="line">    myDesired.setDeltaHeading(-rot);</div><div class="line">    <span class="comment">// return the actionDesired</span></div><div class="line">    <span class="keywordflow">return</span> &amp;myDesired;</div><div class="line">  }</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">  {</div><div class="line">    <span class="comment">// set what we want to do</span></div><div class="line">    myDesired.setVel(0);</div><div class="line">    myDesired.setDeltaHeading(0);</div><div class="line">    <span class="comment">// return the actionDesired</span></div><div class="line">    <span class="keywordflow">return</span> &amp;myDesired;</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">  <a name="a10"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line">  <a name="_a11"></a><a class="code" href="classArArgumentParser.html">ArArgumentParser</a> parser(&amp;argc, argv);</div><div class="line">  parser.<a name="a12"></a><a class="code" href="classArArgumentParser.html#a501694b62a7f8b0c3cadd84aeafe1b35">loadDefaultArguments</a>();</div><div class="line">  <a name="_a13"></a><a class="code" href="classArRobot.html">ArRobot</a> robot;</div><div class="line"></div><div class="line">  <span class="comment">// Connect to the robot, get some initial data from it such as type and name,</span></div><div class="line">  <span class="comment">// and then load parameter files for this robot.</span></div><div class="line">  <a name="_a14"></a><a class="code" href="classArRobotConnector.html">ArRobotConnector</a> robotConnector(&amp;parser, &amp;robot);</div><div class="line">  <span class="keywordflow">if</span>(!robotConnector.<a name="a15"></a><a class="code" href="classArRobotConnector.html#ab192ac393d9cc1e5b61b1a82b08d085d">connectRobot</a>())</div><div class="line">  {</div><div class="line">    <a name="a16"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a17"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;joydriveActionExample: Could not connect to the robot.&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span>(parser.<a name="a18"></a><a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">    {</div><div class="line">        <span class="comment">// -help not given</span></div><div class="line">        <a name="a19"></a><a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">        <a name="a20"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!<a name="a21"></a><a class="code" href="classAria.html#a1b090c01d88bb420b8cf8e0384d25ee1">Aria::parseArgs</a>() || !parser.<a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">  {</div><div class="line">    <a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a22"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;joydriveActionExample: Connected to robot.&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Instance of the JoydriveAction class defined above</span></div><div class="line">  JoydriveAction jdAct;</div><div class="line"></div><div class="line">  <span class="comment">// if the joydrive action couldn&#39;t find the joystick, then exit.</span></div><div class="line">  <span class="keywordflow">if</span> (!jdAct.joystickInited())</div><div class="line">  {</div><div class="line">    printf(<span class="stringliteral">&quot;Do not have a joystick, set up the joystick then rerun the program\n\n&quot;</span>);</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line">  </div><div class="line"></div><div class="line">  <span class="comment">// disable sonar, enable motors, disable amigobot sound</span></div><div class="line">  robot.<a name="a23"></a><a class="code" href="classArRobot.html#aceef80a6274fb34aafcc422e4b1778c1">comInt</a>(<a name="a24"></a><a class="code" href="classArCommands.html#a9a31f904a95bbc31d8b33c6637100cc4a891b21e6cabc8a2296c8bceca20be779">ArCommands::SONAR</a>, 0);</div><div class="line">  robot.<a class="code" href="classArRobot.html#aceef80a6274fb34aafcc422e4b1778c1">comInt</a>(<a name="a25"></a><a class="code" href="classArCommands.html#a9a31f904a95bbc31d8b33c6637100cc4aa8d799cced5def80525936119963aad4">ArCommands::ENABLE</a>, 1);</div><div class="line">  robot.<a class="code" href="classArRobot.html#aceef80a6274fb34aafcc422e4b1778c1">comInt</a>(<a name="a26"></a><a class="code" href="classArCommands.html#a9a31f904a95bbc31d8b33c6637100cc4a7a256695b7937192e88c976d2fd106e5">ArCommands::SOUNDTOG</a>, 0);</div><div class="line"></div><div class="line">  <span class="comment">// add the action</span></div><div class="line">  robot.<a name="a27"></a><a class="code" href="group__easy.html#ga9ab030786d423237d3e4aa06c0e9d8ce">addAction</a>(&amp;jdAct, 100);</div><div class="line"></div><div class="line">  <span class="comment">// run the robot, true so it&#39;ll exit if we lose connection</span></div><div class="line">  robot.<a name="a28"></a><a class="code" href="classArRobot.html#a3c1c3056633caf455c8265259f57f6d7">run</a>(<span class="keyword">true</span>);</div><div class="line">  </div><div class="line">  <span class="comment">// now exit program</span></div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
